package com.coc.service;

import com.coc.bean.*;
import com.coc.bean.def.*;
import com.coc.bean.res.*;
import com.coc.bean.war.*;
import com.coc.dao.*;
import com.coc.util.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONObject;

public class CocMapService extends BaseService {

	// 砍树挖石头
	// type,index
	// 改成客户端判定砍完后通知服务器
	// 只要验算收到通知时刻有没工人,然后用随机数因子计算是否有宝石
	public void MapCut(HttpServletRequest request, JSONObject ht)
			throws Exception {

		// T0,1,2,3...tree类型
		String type = getParamValue("type", ht, request);
		long t = Long.parseLong(getParamValue("t", ht, request));
		int index = Integer.parseInt(getParamValue("id", ht, request));
		int id = Integer.parseInt(request.getParameter("tk").substring(0,
				request.getParameter("tk").indexOf("_")));
		CcMap cm = (CcMap) MemCacheUtil.getObject("cm_" + id);
		CcEloUser ceu = getEloUserDAO(request).findByID(id);
		// t比动作时间大,说明已经运行过了,本次可能因为网络问题导致上次动作包没收到回执
		// 客户端把2次动作间隔之间的动作包一起打包,重新发包,直接跳过就好
		// 比now大,作弊
		if (t > System.currentTimeMillis()) {
			throw new SignException("act_cut:t>now", 2);
		}
		if (t < ceu.getLastaction()) {
			return;
		}
		ceu.setLastaction(t);
		getEloUserDAO(request).saveOrUpdate(ceu);
		Building bu = getBuildSettingXML(request).findBuByID(type);
		String str = (String) bu.getLv().get(0);
		String[] strs = str.split(";");
		// 类型;需求等级(HT/User);花费;建造时间;矿脉容量/dect总数;生产速率;建造经验;加速费用;
		int exp = Integer.parseInt(strs[6]);
		// 需要检测工人
		initBuildingStatus(cm, t, request);
		if (cm.getSlavs().size() <= cm.getWorkend().size()) {
			throw new SignException("act_cut:not enough worker", 2);
		}
		// 砍掉,Tree里面去掉,map中位置占用也取消(地图边缘要设置成2,内部=0)
		Hashtable<String, List<Tree>> trees = cm.getTrees();
		if (trees == null) {
			throw new SignException("act_cut:not tree to cut", 2);
		}
		if (trees.get(bu.getType()) == null) {
			trees.put(bu.getType(), new ArrayList<Tree>());
		}
		List<Tree> treeList = trees.get(type);
		Tree tmp = treeList.get(index);
		treeList.remove(index);
		if (treeList.size() != 0) {
			trees.put(type, treeList);
		} else {
			trees.remove(type);
		}
		cm.setTrees(trees);
	
		// 经验只保存在ccuser中,等级ccuser和CcEloUser都保存
		// todo 随机给gem
		CcUser cu = cm.getCcuser();
		cu.setExp(cu.getExp() + exp);
		cm.setCcuser(cu);
		MemCacheUtil.setObject("cm_" + cm.getUid(), cm);
	}

	// 卖建筑物
	// 只能卖DECT和BOMB
	// 要减totalUnitSize
	public void MapSell(HttpServletRequest request, JSONObject ht)
			throws Exception {

		String type = getParamValue("type", ht, request);
		int index = Integer.parseInt(getParamValue("id", ht, request));
		long t = Long.parseLong(getParamValue("t", ht, request));
		int id = Integer.parseInt(request.getParameter("tk").substring(0,
				request.getParameter("tk").indexOf("_")));
		CcMap cm = (CcMap) MemCacheUtil.getObject("cm_" + id);
		CcEloUser ceu = getEloUserDAO(request).findByID(id);
		// t比动作时间大,说明已经运行过了,本次可能因为网络问题导致上次动作包没收到回执
		// 客户端把2次动作间隔之间的动作包一起打包,重新发包,直接跳过就好
		// 比now大,作弊
		if (t > System.currentTimeMillis()) {
			throw new SignException("act_sell:t>now", 2);
		}
		if (t < ceu.getLastaction()) {
			return;
		}
		ceu.setLastaction(t);
		getEloUserDAO(request).saveOrUpdate(ceu);
		Building bu = getBuildSettingXML(request).findBuByID(type);
		// 不能卖
		if (bu == null || bu.getSold().length() < 1) {
			throw new SignException("act_sell:can not sell", 2);
		}
		String sold = bu.getSold();
		CcUser cu = cm.getCcuser();
		String str = (String) bu.getLv().get(0);
		String[] strs = str.split(";");
		// 类型;需求等级(HT/User);花费;建造时间;矿脉容量/dect总数;生产速率;建造经验;加速费用;
		String[] recost = strs[2].split(",");
		// 退钱
		int size = Integer.parseInt(bu.getSize());
		for (int i = 0; i < recost.length; i++) {
			int value = (int) (Float.parseFloat(sold) * Integer
					.parseInt(recost[i].substring(1)));
			if (recost[i].startsWith("g"))
				cu.setGold(cu.getGold().intValue() + value);
			else if (recost[i].startsWith("e"))
				cu.setElixir(cu.getElixir().intValue() + value);
			else if (recost[i].startsWith("d"))
				cu.setDark(cu.getDark().intValue() + value);
			else if (recost[i].startsWith("m"))
				cu.setGem(cu.getGem().intValue() + value);
		}
		// DECT特殊处理
		if (type.length() < 4 && type.startsWith("D")) {
			Hashtable<String, List<Dect>> list = cm.getDects();
			if (list == null) {
				list = new Hashtable<String, List<Dect>>();
			}
			List<Dect> dects = list.get(type);
			if (dects == null) {
				dects = new ArrayList<Dect>();
			}
			Dect tmp = dects.get(index);
			
			cm.setTotalUnitSize(cm.getTotalUnitSize() - size * size);
			dects.remove(index);
			if (dects.size() > 0) {
				list.put(type, dects);
			} else {
				list.remove(type);
			}
			cm.setDects(list);
		} else if (type.equals("BOMB")) {
			List<Bomb> list = cm.getBombs();
			Bomb tmp = list.get(index);
			// 地图数据中移除,减total数量
			
			cm.setTotalUnitSize(cm.getTotalUnitSize() - size * size);
			cm.getBombs().remove(index);
		} else if (type.equals("GITB")) {
			List<Gitb> list = cm.getGitbs();
			Gitb tmp = list.get(index);
			// 地图数据中移除,减total数量
			
			cm.setTotalUnitSize(cm.getTotalUnitSize() - size * size);
			cm.getGitbs().remove(index);
		} else if (type.equals("TRAP")) {
			List<Trap> list = cm.getTraps();
			Trap tmp = list.get(index);
			// 地图数据中移除,减total数量
			
			cm.setTotalUnitSize(cm.getTotalUnitSize() - size * size);
			cm.getTraps().remove(index);
		} else if (type.equals("AIRB")) {
			List<Airb> list = cm.getAirbs();
			Airb tmp = list.get(index);
			// 地图数据中移除,减total数量
			
			cm.setTotalUnitSize(cm.getTotalUnitSize() - size * size);
			cm.getAirbs().remove(index);
		} else if (type.equals("AIRM")) {
			List<Airm> list = cm.getAirms();
			Airm tmp = list.get(index);
			// 地图数据中移除,减total数量
			
			cm.setTotalUnitSize(cm.getTotalUnitSize() - size * size);
			cm.getAirms().remove(index);
		}

		cm.setCcuser(cu);
		MemCacheUtil.setObject("cm_" + cm.getUid().intValue(), cm);

	}

	// 建造建筑物
	// t - 服务器时间
	// type - 建筑物类型
	// x - 坐标x
	// y - 坐标y
	// 建造时间是负数的不需要工人,0需要工人不占用工人
	// 建造成功后要加totalUnitSize

	public void MapBuild(HttpServletRequest request, JSONObject ht)
			throws Exception {

		String type = getParamValue("type", ht, request);
		long t = Long.parseLong(getParamValue("t", ht, request));
		int x = Integer.parseInt(getParamValue("x", ht, request));
		int y = Integer.parseInt(getParamValue("y", ht, request));
		int id = Integer.parseInt(request.getParameter("tk").substring(0,
				request.getParameter("tk").indexOf("_")));

		CcMap cm = (CcMap) MemCacheUtil.getObject("cm_" + id);
		CcEloUser ceu = getEloUserDAO(request).findByID(id);
		// t比动作时间大,说明已经运行过了,本次可能因为网络问题导致上次动作包没收到回执
		// 客户端把2次动作间隔之间的动作包一起打包,重新发包,直接跳过就好
		// 比now大,作弊
		if (t > System.currentTimeMillis()) {
			throw new SignException("act_build:t>now", 2);
		}
		if (t < ceu.getLastaction()) {
			return;
		}
		ceu.setLastaction(t);
		getEloUserDAO(request).saveOrUpdate(ceu);
		// 检测工人,空地,花费 
		if (!CheckBuild(request, ht, cm)) {
			throw new SignException("act_bulid:can not build", 2);
		}
		// DECT特殊处理
		if (type.length() < 4 && type.startsWith("D")) {
			// DECT里面拆分成D1,2,3,4等各个子类,用hashtable保存
			Hashtable<String, List<Dect>> list = cm.getDects();
			if (list == null) {
				list = new Hashtable<String, List<Dect>>();
			}
			List<Dect> dects = list.get(type);
			if (dects == null) {
				dects = new ArrayList<Dect>();
			}
			Dect tmp = new Dect();
			tmp.setX(x);
			tmp.setY(y);
			dects.add(tmp);
			list.put(type, dects);
			cm.setDects(list);
		} else if (type.startsWith("WALL")) {
			int typeid = 1;
			List<Wall> list = cm.getWalls();
			if (list == null) {
				list = new ArrayList<Wall>();
			}
			Wall tmp = new Wall();
			tmp.setLv(typeid);
			tmp.setX(x);
			tmp.setY(y);
			tmp.setLv(1);
			list.add(tmp);
			cm.setWalls(list);
		} else if (type.startsWith("SLAV")) {
			List<Slav> list = cm.getSlavs();
			if (list == null) {
				list = new ArrayList<Slav>();
			}
			Slav tmp = new Slav();
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setSlavs(list);
		} else if (type.startsWith("CAST")) {
			if (cm.getCast() == null) {
				Cast tmp = new Cast();
				tmp.setUpgradeBeg(t);
				tmp.setUpgradeEnd(cm.getLastWorkend());
				
				tmp.setX(x);
				tmp.setY(y);
				cm.setCast(tmp);
			}
		} else if (type.startsWith("HALL")) {
			if (cm.getHall() == null) {
				Hall tmp = new Hall();
				tmp.setUpgradeBeg(t);
				tmp.setUpgradeEnd(cm.getLastWorkend());
				
				tmp.setX(x);
				tmp.setY(y);
				cm.setHall(tmp);
			}
		} else if (type.startsWith("LABT")) {
			if (cm.getLabt() == null) {
				Labt tmp = new Labt();
				tmp.setUpgradeBeg(t);
				tmp.setUpgradeEnd(cm.getLastWorkend());
				
				tmp.setX(x);
				tmp.setY(y);
				cm.setLabt(tmp);
			}
		} else if (type.startsWith("SPEL")) {
			if (cm.getSpel() == null) {
				Spel tmp = new Spel();
				tmp.setUpgradeBeg(t);
				tmp.setUpgradeEnd(cm.getLastWorkend());
				
				tmp.setX(x);
				tmp.setY(y);
				cm.setSpel(tmp);
			}
		} else if (type.startsWith("KING")) {
			if (cm.getKing() == null) {
				King tmp = new King();
				tmp.setUpgradeBeg(t);
				tmp.setUpgradeEnd(cm.getLastWorkend());
				
				tmp.setX(x);
				tmp.setY(y);
				cm.setKing(tmp);
			}
		} else if (type.startsWith("QUEN")) {
			if (cm.getQuen() == null) {
				Quen tmp = new Quen();
				tmp.setUpgradeBeg(t);
				tmp.setUpgradeEnd(cm.getLastWorkend());
				
				tmp.setX(x);
				tmp.setY(y);
				cm.setQuen(tmp);
			}
		} else if (type.startsWith("ELXM")) {
			List<Elxm> list = cm.getElxms();
			if (list == null) {
				list = new ArrayList<Elxm>();
			}
			Elxm tmp = new Elxm();
			tmp.setUpgradeBeg(t);
			tmp.setUpgradeEnd(cm.getLastWorkend());
			
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setElxms(list);
		} else if (type.startsWith("ELXS")) {
			List<Elxs> list = cm.getElxss();
			if (list == null) {
				list = new ArrayList<Elxs>();
			}
			Elxs tmp = new Elxs();
			tmp.setUpgradeBeg(t);
			tmp.setUpgradeEnd(cm.getLastWorkend());
			
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setElxss(list);
		} else if (type.startsWith("GOLM")) {
			List<Golm> list = cm.getGolms();
			if (list == null) {
				list = new ArrayList<Golm>();
			}
			Golm tmp = new Golm();
			tmp.setUpgradeBeg(t);
			tmp.setUpgradeEnd(cm.getLastWorkend());
			
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setGolms(list);
		} else if (type.startsWith("GOLS")) {
			List<Gols> list = cm.getGolss();
			if (list == null) {
				list = new ArrayList<Gols>();
			}
			Gols tmp = new Gols();
			tmp.setUpgradeBeg(t);
			tmp.setUpgradeEnd(cm.getLastWorkend());
			
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setGolss(list);
		} else if (type.startsWith("DAKM")) {
			List<Dakm> list = cm.getDakms();
			if (list == null) {
				list = new ArrayList<Dakm>();
			}
			Dakm tmp = new Dakm();
			tmp.setUpgradeBeg(t);
			tmp.setUpgradeEnd(cm.getLastWorkend());
			
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setDakms(list);
		} else if (type.startsWith("DAKS")) {
			List<Daks> list = cm.getDakss();
			if (list == null) {
				list = new ArrayList<Daks>();
			}
			Daks tmp = new Daks();
			tmp.setUpgradeBeg(t);
			tmp.setUpgradeEnd(cm.getLastWorkend());
			
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setDakss(list);
		} else if (type.startsWith("CAMP")) {
			List<Camp> list = cm.getCamps();
			if (list == null) {
				list = new ArrayList<Camp>();
			}
			Camp tmp = new Camp();
			tmp.setUpgradeBeg(t);
			tmp.setUpgradeEnd(cm.getLastWorkend());
			
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setCamps(list);
		} else if (type.startsWith("BARK")) {
			List<Tran> list = cm.getBarks();
			if (list == null) {
				list = new ArrayList<Tran>();
			}
			Tran tmp = new Tran();
			tmp.setUpgradeBeg(t);
			tmp.setUpgradeEnd(cm.getLastWorkend());
			
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setBarks(list);
		} else if (type.startsWith("BKDK")) {
			List<Trdk> list = cm.getBkdks();
			if (list == null) {
				list = new ArrayList<Trdk>();
			}
			Trdk tmp = new Trdk();
			tmp.setUpgradeBeg(t);
			tmp.setUpgradeEnd(cm.getLastWorkend());
			
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setBkdks(list);
		} else if (type.startsWith("CANN")) {
			List<Cann> list = cm.getCanns();
			if (list == null) {
				list = new ArrayList<Cann>();
			}
			Cann tmp = new Cann();
			tmp.setUpgradeBeg(t);
			tmp.setUpgradeEnd(cm.getLastWorkend());
			
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setCanns(list);
		} else if (type.startsWith("ARCT")) {
			List<Arct> list = cm.getArcts();
			if (list == null) {
				list = new ArrayList<Arct>();
			}
			Arct tmp = new Arct();
			tmp.setUpgradeBeg(t);
			tmp.setUpgradeEnd(cm.getLastWorkend());
			
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setArcts(list);
		} else if (type.startsWith("MORT")) {
			List<Mort> list = cm.getMorts();
			if (list == null) {
				list = new ArrayList<Mort>();
			}
			Mort tmp = new Mort();
			tmp.setUpgradeBeg(t);
			tmp.setUpgradeEnd(cm.getLastWorkend());
			
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setMorts(list);
		} else if (type.startsWith("AIRD")) {
			List<Aird> list = cm.getAirds();
			if (list == null) {
				list = new ArrayList<Aird>();
			}
			Aird tmp = new Aird();
			tmp.setUpgradeBeg(t);
			tmp.setUpgradeEnd(cm.getLastWorkend());
			
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setAirds(list);
		} else if (type.startsWith("WIZT")) {
			List<Wizt> list = cm.getWizts();
			if (list == null) {
				list = new ArrayList<Wizt>();
			}
			Wizt tmp = new Wizt();
			tmp.setUpgradeBeg(t);
			tmp.setUpgradeEnd(cm.getLastWorkend());
			
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setWizts(list);
		} else if (type.startsWith("TSLA")) {
			List<Hide> list = cm.getTslas();
			if (list == null) {
				list = new ArrayList<Hide>();
			}
			Hide tmp = new Hide();
			tmp.setUpgradeBeg(t);
			tmp.setUpgradeEnd(cm.getLastWorkend());
			
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setTslas(list);
		} else if (type.startsWith("XBOW")) {
			List<Xbow> list = cm.getXbows();
			if (list == null) {
				list = new ArrayList<Xbow>();
			}
			Xbow tmp = new Xbow();
			tmp.setUpgradeBeg(t);
			tmp.setUpgradeEnd(cm.getLastWorkend());
			
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setXbows(list);
		} else if (type.startsWith("INFT")) {
			List<Inft> list = cm.getInfts();
			if (list == null) {
				list = new ArrayList<Inft>();
			}
			Inft tmp = new Inft();
			tmp.setUpgradeBeg(t);
			tmp.setUpgradeEnd(cm.getLastWorkend());
			
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setInfts(list);
		} else if (type.startsWith("BOMB")) {
			List<Bomb> list = cm.getBombs();
			if (list == null) {
				list = new ArrayList<Bomb>();
			}
			Bomb tmp = new Bomb();
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setBombs(list);
		} else if (type.startsWith("TRAP")) {
			List<Trap> list = cm.getTraps();
			if (list == null) {
				list = new ArrayList<Trap>();
			}
			Trap tmp = new Trap();
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setTraps(list);
		} else if (type.startsWith("GITB")) {
			List<Gitb> list = cm.getGitbs();
			if (list == null) {
				list = new ArrayList<Gitb>();
			}
			Gitb tmp = new Gitb();
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setGitbs(list);
		} else if (type.startsWith("AIRB")) {
			List<Airb> list = cm.getAirbs();
			if (list == null) {
				list = new ArrayList<Airb>();
			}
			Airb tmp = new Airb();
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setAirbs(list);
		} else if (type.startsWith("AIRM")) {
			List<Airm> list = cm.getAirms();
			if (list == null) {
				list = new ArrayList<Airm>();
			}
			Airm tmp = new Airm();
			tmp.setX(x);
			tmp.setY(y);
			list.add(tmp);
			cm.setAirms(list);
		}
		MemCacheUtil.setObject("cm_" + cm.getUid().intValue(), cm);
	}

	// 升级建筑物
	// t - 服务器时间
	// type - 建筑物类型
	// id - 在本建筑物类型内的序号(序号从 0 开始)
	public void MapUpgrade(HttpServletRequest request, JSONObject ht)
			throws Exception {

		String type = getParamValue("type", ht, request);
		long t = Long.parseLong(getParamValue("t", ht, request));
		int index = Integer.parseInt(getParamValue("id", ht, request));
		int id = Integer.parseInt(request.getParameter("tk").substring(0,
				request.getParameter("tk").indexOf("_")));
		CcMap cm = (CcMap) MemCacheUtil.getObject("cm_" + id);
		CcEloUser ceu = getEloUserDAO(request).findByID(id);
		// t比动作时间大,说明已经运行过了,本次可能因为网络问题导致上次动作包没收到回执
		// 客户端把2次动作间隔之间的动作包一起打包,重新发包,直接跳过就好
		// 比now大,作弊
		if (t > System.currentTimeMillis()) {
			throw new SignException("act_upgrade:t>now", 2);
		}
		if (t < ceu.getLastaction()) {
			return;
		}
		ceu.setLastaction(t);
		getEloUserDAO(request).saveOrUpdate(ceu);
		// 检测升级前置条件,工人够不够
		if (!CheckUpgrade(request, ht, cm)) {
			throw new SignException("act_upgrade:can not upgrade", 2);
		}
		CcBuilding tmp = null;
		if (type.startsWith("WALL")) {
			tmp = cm.getWalls().get(index);
		} else if (type.startsWith("CAST")) {
			tmp = cm.getCast();
		} else if (type.startsWith("HALL")) {
			tmp = cm.getHall();
		} else if (type.startsWith("LABT")) {
			tmp = cm.getLabt();
		} else if (type.startsWith("SPEL")) {
			tmp = cm.getSpel();
		} else if (type.startsWith("KING")) {
			tmp = cm.getKing();
		} else if (type.startsWith("QUEN")) {
			tmp = cm.getQuen();
		} else if (type.startsWith("ELXM")) {
			tmp = cm.getElxms().get(index);
		} else if (type.startsWith("ELXS")) {
			tmp = cm.getElxss().get(index);
		} else if (type.startsWith("GOLM")) {
			tmp = cm.getGolms().get(index);
		} else if (type.startsWith("GOLS")) {
			tmp = cm.getGolss().get(index);
		} else if (type.startsWith("DAKM")) {
			tmp = cm.getDakms().get(index);
		} else if (type.startsWith("DAKS")) {
			tmp = cm.getDakss().get(index);
		} else if (type.startsWith("CAMP")) {
			tmp = cm.getCamps().get(index);
		} else if (type.startsWith("BARK")) {
			tmp = cm.getBarks().get(index);
		} else if (type.startsWith("BKDK")) {
			tmp = cm.getBkdks().get(index);
		} else if (type.startsWith("CANN")) {
			tmp = cm.getCanns().get(index);
		} else if (type.startsWith("ARCT")) {
			tmp = cm.getArcts().get(index);
		} else if (type.startsWith("MORT")) {
			tmp = cm.getMorts().get(index);
		} else if (type.startsWith("AIRD")) {
			tmp = cm.getAirds().get(index);
		} else if (type.startsWith("WIZT")) {
			tmp = cm.getWizts().get(index);
		} else if (type.startsWith("TSLA")) {
			tmp = cm.getTslas().get(index);
		} else if (type.startsWith("XBOW")) {
			tmp = cm.getXbows().get(index);
		} else if (type.startsWith("INFT")) {
			tmp = cm.getInfts().get(index);
		}
		// 检测资源和城镇中心等,如果通过直接扣资源
		this.CheckUpgradeRes(request, t, type, tmp, cm);
		tmp.setUpgradeEnd(cm.getLastWorkend());
		tmp.setUpgradeBeg(t);
		this.initBuildingStatus(cm, t, request);
		MemCacheUtil.setObject("cm_" + cm.getUid(), cm);
	}

	// 升级建筑秒杀
	// 新修改,这边要lv+1
	public void MapBoost(HttpServletRequest request, JSONObject ht)
			throws Exception {

		long t = Long.parseLong(getParamValue("t", ht, request));
		int index = Integer.parseInt(getParamValue("id", ht, request));
		String type = getParamValue("type", ht, request);
		int id = Integer.parseInt(request.getParameter("tk").substring(0,
				request.getParameter("tk").indexOf("_")));
		CcMap cm = (CcMap) MemCacheUtil.getObject("cm_" + id);
		CcEloUser ceu = getEloUserDAO(request).findByID(id);
		// t比动作时间大,说明已经运行过了,本次可能因为网络问题导致上次动作包没收到回执
		// 客户端把2次动作间隔之间的动作包一起打包,重新发包,直接跳过就好
		// 比now大,作弊
		if (t > System.currentTimeMillis()) {
			throw new SignException("act_buildboost:t>now", 2);
		}
		if (t < ceu.getLastaction()) {
			return;
		}
		int needgem = 0;
		ceu.setLastaction(t);
		getEloUserDAO(request).saveOrUpdate(ceu);
		CcUser cu = cm.getCcuser();
		CcBuilding tmp = null;
		if (type.startsWith("CAST")) {
			tmp = cm.getCast();
		} else if (type.startsWith("HALL")) {
			tmp = cm.getHall();
		} else if (type.startsWith("LABT")) {
			tmp = cm.getLabt();
		} else if (type.startsWith("SPEL")) {
			tmp = cm.getSpel();
		} else if (type.startsWith("KING")) {
			tmp = cm.getKing();
		} else if (type.startsWith("QUEN")) {
			tmp = cm.getQuen();
		} else if (type.startsWith("ELXM")) {
			tmp = (Elxm) cm.getElxms().get(index);
		} else if (type.startsWith("ELXS")) {
			tmp = (Elxs) cm.getElxss().get(index);
		} else if (type.startsWith("GOLM")) {
			tmp = (Golm) cm.getGolms().get(index);
		} else if (type.startsWith("GOLS")) {
			tmp = (Gols) cm.getGolss().get(index);
		} else if (type.startsWith("DAKM")) {
			tmp = (Dakm) cm.getDakms().get(index);
		} else if (type.startsWith("DAKS")) {
			tmp = (Daks) cm.getDakss().get(index);
		} else if (type.startsWith("CAMP")) {
			tmp = (Camp) cm.getCamps().get(index);
		} else if (type.startsWith("BARK")) {
			tmp = (Tran) cm.getBarks().get(index);
		} else if (type.startsWith("BKDK")) {
			tmp = (Trdk) cm.getBkdks().get(index);
		} else if (type.startsWith("CANN")) {
			tmp = (Cann) cm.getCanns().get(index);
		} else if (type.startsWith("ARCT")) {
			tmp = (Arct) cm.getArcts().get(index);
		} else if (type.startsWith("MORT")) {
			tmp = (Mort) cm.getMorts().get(index);
		} else if (type.startsWith("AIRD")) {
			tmp = (Aird) cm.getAirds().get(index);
		} else if (type.startsWith("WIZT")) {
			tmp = (Wizt) cm.getWizts().get(index);
		} else if (type.startsWith("TSLA")) {
			tmp = (Hide) cm.getTslas().get(index);
		} else if (type.startsWith("XBOW")) {
			tmp = (Xbow) cm.getXbows().get(index);
		} else if (type.startsWith("INFT")) {
			tmp = (Inft) cm.getInfts().get(index);
		}
		needgem = this.getNeedGemFromMS(request, (tmp.getUpgradeEnd() - t));
		if (needgem <= 0) {
			throw new SignException("act_buildboost:not need boost", 2);
		}
		if (cu.getGem().intValue() < needgem) {
			throw new SignException("act_buildboost:not enough gem", 2);
		}
		cu.setGem(Integer.valueOf(cu.getGem().intValue() - needgem));
		cm.resetWorkend(tmp.getUpgradeEnd(), t);
		tmp.setUpgradeEnd(0);
		tmp.setUpgradeBeg(0);
		MemCacheUtil.setObject("cm_" + cm.getUid(), cm);
	}
	// 移动
	// t - 服务器时间
	// type - 建筑物类型
	// id - 在本建筑物类型内的序号(序号从 0 开始)
	// x,y - 目的地坐标
	public void MapMove(HttpServletRequest request, JSONObject ht)
			throws Exception {
		String type = getParamValue("type", ht, request);
		long t = Long.parseLong(getParamValue("t", ht, request));
		int x = Integer.parseInt(getParamValue("x", ht, request));
		int y = Integer.parseInt(getParamValue("y", ht, request));
		int index = Integer.parseInt(getParamValue("id", ht, request));
		int id = Integer.parseInt(request.getParameter("tk").substring(0,
				request.getParameter("tk").indexOf("_")));
		CcMap cm = (CcMap) MemCacheUtil.getObject("cm_" + id);
		CcEloUser ceu = getEloUserDAO(request).findByID(id);
		// t比动作时间大,说明已经运行过了,本次可能因为网络问题导致上次动作包没收到回执
		// 客户端把2次动作间隔之间的动作包一起打包,重新发包,直接跳过就好
		// 比now大,作弊
		if (t > System.currentTimeMillis()) {
			throw new SignException("act_move:t>now", 2);
		}
		if (t < ceu.getLastaction()) {
			return;
		}
		ceu.setLastaction(t);
		getEloUserDAO(request).saveOrUpdate(ceu);
		CcBuilding tmp=null;
		// DECT特殊处理
		if (type.length() < 4 && type.startsWith("D")) {
			tmp = cm.getDects().get(type).get(index);
		} else if (type.startsWith("SLAV")) {
			tmp = cm.getSlavs().get(index);	
		} else if (type.startsWith("WALL")) {
			tmp = cm.getWalls().get(index);
		} else if (type.startsWith("CAST")) {
			tmp = cm.getCast();
		} else if (type.startsWith("HALL")) {
			tmp = cm.getHall();
		} else if (type.startsWith("LABT")) {
			tmp = cm.getLabt();
		} else if (type.startsWith("SPEL")) {
			tmp = cm.getSpel();
		} else if (type.startsWith("KING")) {
			tmp = cm.getKing();
		} else if (type.startsWith("QUEN")) {
			tmp = cm.getQuen();
		} else if (type.startsWith("ELXM")) {
			tmp = cm.getElxms().get(index);
		} else if (type.startsWith("ELXS")) {
			tmp = cm.getElxss().get(index);
		} else if (type.startsWith("GOLM")) {
			tmp = cm.getGolms().get(index);
		} else if (type.startsWith("GOLS")) {
			tmp = cm.getGolss().get(index);
		} else if (type.startsWith("DAKM")) {
			tmp = cm.getDakms().get(index);
		} else if (type.startsWith("DAKS")) {
			tmp = cm.getDakss().get(index);
		} else if (type.startsWith("CAMP")) {
			tmp = cm.getCamps().get(index);
		} else if (type.startsWith("BARK")) {
			tmp = cm.getBarks().get(index);
		} else if (type.startsWith("BKDK")) {
			tmp = cm.getBkdks().get(index);
		} else if (type.startsWith("CANN")) {
			tmp = cm.getCanns().get(index);
		} else if (type.startsWith("ARCT")) {
			tmp = cm.getArcts().get(index);
		} else if (type.startsWith("MORT")) {
			tmp = cm.getMorts().get(index);
		} else if (type.startsWith("AIRD")) {
			tmp = cm.getAirds().get(index);
		} else if (type.startsWith("WIZT")) {
			tmp = cm.getWizts().get(index);
		} else if (type.startsWith("TSLA")) {
			tmp = cm.getTslas().get(index);
		} else if (type.startsWith("Inft")) {
			tmp = cm.getInfts().get(index);
		} else if (type.startsWith("XBOW")) {
			tmp = cm.getXbows().get(index);
		} else if (type.startsWith("BOMB")) {
			tmp = cm.getBombs().get(index);
		} else if (type.startsWith("TRAP")) {
			tmp = cm.getTraps().get(index);
		} else if (type.startsWith("GITB")) {
			tmp = cm.getGitbs().get(index);
		} else if (type.startsWith("AIRB")) {
			tmp = cm.getAirbs().get(index);
		} else if (type.startsWith("AIRM")) {
			tmp = cm.getAirms().get(index);
		}
		tmp.setX(x);
		tmp.setY(y);
		MemCacheUtil.setObject("cm_" + cm.getUid(), cm);
	}

	// 取消升级  --- 新建建筑不能取消
	// t - 服务器时间
	// type - 建筑物类型
	// id - 在本建筑物类型内的序号(序号从 0 开始)
	public void MapCancel(HttpServletRequest request, JSONObject ht)
			throws Exception {

		String type = getParamValue("type", ht, request);
		long t = Long.parseLong(getParamValue("t", ht, request));
		int index = Integer.parseInt(getParamValue("id", ht, request));
		// 退钱数量
		String recost = "";
		Building bu = getBuildSettingXML(request).findBuByID(type);
		int id = Integer.parseInt(request.getParameter("tk").substring(0,
				request.getParameter("tk").indexOf("_")));
		CcMap cm = (CcMap) MemCacheUtil.getObject("cm_" + id);
		CcEloUser ceu = getEloUserDAO(request).findByID(id);
		// t比动作时间大,说明已经运行过了,本次可能因为网络问题导致上次动作包没收到回执
		// 客户端把2次动作间隔之间的动作包一起打包,重新发包,直接跳过就好
		// 比now大,作弊
		if (t > System.currentTimeMillis()) {
			throw new SignException("act_buildcancel:t>now", 2);
		}
		if (t < ceu.getLastaction()) {
			return;
		}
		ceu.setLastaction(t);
		getEloUserDAO(request).saveOrUpdate(ceu);
		CcUser cu = cm.getCcuser();
		CcBuilding tmp = null;
		if (type.startsWith("WALL")) {
			tmp = cm.getWalls().get(index);
		} else if (type.startsWith("CAST")) {
			tmp = cm.getCast();
		} else if (type.startsWith("HALL")) {
			tmp = cm.getHall();
		} else if (type.startsWith("LABT")) {
			tmp = cm.getLabt();
		} else if (type.startsWith("SPEL")) {
			tmp = cm.getSpel();
		} else if (type.startsWith("KING")) {
			tmp = cm.getKing();
		} else if (type.startsWith("QUEN")) {
			tmp = cm.getQuen();
		} else if (type.startsWith("ELXM")) {
			tmp = cm.getElxms().get(index);
		} else if (type.startsWith("ELXS")) {
			tmp = cm.getElxss().get(index);
		} else if (type.startsWith("GOLM")) {
			tmp = cm.getGolms().get(index);
		} else if (type.startsWith("GOLS")) {
			tmp = cm.getGolss().get(index);
		} else if (type.startsWith("DAKM")) {
			tmp = cm.getDakms().get(index);
		} else if (type.startsWith("DAKS")) {
			tmp = cm.getDakss().get(index);
		} else if (type.startsWith("CAMP")) {
			tmp = cm.getCamps().get(index);
		} else if (type.startsWith("BARK")) {
			tmp = cm.getBarks().get(index);
		} else if (type.startsWith("BKDK")) {
			tmp = cm.getBkdks().get(index);
		} else if (type.startsWith("CANN")) {
			tmp = cm.getCanns().get(index);
		} else if (type.startsWith("ARCT")) {
			tmp = cm.getArcts().get(index);
		} else if (type.startsWith("MORT")) {
			tmp = cm.getMorts().get(index);
		} else if (type.startsWith("AIRD")) {
			tmp = cm.getAirds().get(index);
		} else if (type.startsWith("WIZT")) {
			tmp = cm.getWizts().get(index);
		} else if (type.startsWith("TSLA")) {
			tmp = cm.getTslas().get(index);
		} else if (type.startsWith("XBOW")) {
			tmp = cm.getXbows().get(index);
		} else if (type.startsWith("INFT")) {
			tmp = cm.getInfts().get(index);
		}
		if (tmp.getUpgradeEnd() != 0 && tmp.getUpgradeEnd() > t) {
			cm.removeWorkend(tmp.getUpgradeEnd());
			String str = (String) bu.getLv().get(tmp.getLv());
			String[] strs = str.split(";");
			recost = strs[2];
			tmp.setUpgradeBeg(0L);
			tmp.setUpgradeEnd(0L);
		}else{
			throw new SignException("act_buildcancel:upgrade is done", 2);
		}

		if (recost.length() > 0) {
			String[] cost = recost.split(",");
			for (int i = 0; i < cost.length; i++) {
				if (cost[i].startsWith("g"))
					cu.setGold(Integer.valueOf(cu.getGold().intValue()
							+ Integer.parseInt(cost[i].substring(1)) / 2));
				else if (cost[i].startsWith("e"))
					cu.setElixir(Integer.valueOf(cu.getElixir().intValue()
							+ Integer.parseInt(cost[i].substring(1)) / 2));
				else if (cost[i].startsWith("d"))
					cu.setDark(Integer.valueOf(cu.getDark().intValue()
							+ Integer.parseInt(cost[i].substring(1)) / 2));
				else if (cost[i].startsWith("m")) {
					cu.setGem(Integer.valueOf(cu.getGem().intValue()
							+ Integer.parseInt(cost[i].substring(1)) / 2));
				}
			}
		}	
		MemCacheUtil.setObject("cm_" + cm.getUid(), cm);

	}

	// 检测升级资源和城镇中心等级,同时更新工人和经验列表
	private void CheckUpgradeRes(HttpServletRequest request, long t,
			String type, CcBuilding tmp, CcMap cm) throws Exception {

		Building bu = getBuildSettingXML(request).findBuByID(type);
		// 类型;需求等级(HT/User);花费;建造时间;矿脉容量/dect总数;生产速率;建造经验;加速费用;
		// bu.getLv() 保存该建筑物各级数据,0-x对应 1-(x+1)等级,升级默认取当前lv
		String[] strs = ((String) bu.getLv().get(tmp.getLv())).split(";");
		// 花费
		String[] cost = strs[2].split(",");
		int gold = 0;
		int elxm = 0;
		int dakm = 0;
		int gem = 0;
		for (int i = 0; i < cost.length; i++) {
			if (cost[i].startsWith("g"))
				gold = Integer.parseInt(cost[i].substring(1));
			else if (cost[i].startsWith("e"))
				elxm = Integer.parseInt(cost[i].substring(1));
			else if (cost[i].startsWith("d"))
				dakm = Integer.parseInt(cost[i].substring(1));
			else if (cost[i].startsWith("m")) {
				gem = Integer.parseInt(cost[i].substring(1));
			}
		}
		int htlv = Integer.parseInt(strs[1]);
		CcUser cu = cm.getCcuser();
		// 城镇中心等级不够
		if (htlv > cu.getHtlv().intValue()) {
			throw new SignException("act_upgrade:not enough ht lv", 2);
		}
		if (cu.getGold().intValue() < gold) {
			// 资源永远只有一种,非gem资源不够直接扣gem
			int addgem = this.getNeedGemFromGold(request, (gold - cu.getGold()
					.intValue()));
			if (cu.getGem().intValue() < addgem) {
				throw new SignException("act_upgrade:not enough gold and gem",
						2);
			} else {
				cu.setGold(0);
				cu.setGem(Integer.valueOf(cu.getGem().intValue() - addgem));
			}
		} else {
			cu.setGold(Integer.valueOf(cu.getGold().intValue() - gold));
		}

		if (cu.getElixir().intValue() < elxm) {
			int addgem = this.getNeedGemFromElxm(request, (elxm - cu
					.getElixir().intValue()));
			if (cu.getGem().intValue() < addgem) {
				throw new SignException("act_upgrade:not enough elxm and gem",
						2);
			} else {
				cu.setElixir(0);
				cu.setGem(Integer.valueOf(cu.getGem().intValue() - addgem));
			}
		} else {
			cu.setElixir(Integer.valueOf(cu.getElixir().intValue() - elxm));
		}

		if (cu.getDark().intValue() < dakm) {
			int addgem = this.getNeedGemFromDakm(request, (dakm - cu.getDark()
					.intValue()));
			if (cu.getGem().intValue() < addgem) {
				throw new SignException("act_upgrade:not enough dakm and gem",
						2);
			} else {
				cu.setDark(0);
				cu.setGem(Integer.valueOf(cu.getGem().intValue() - addgem));
			}
		} else {
			cu.setDark(Integer.valueOf(cu.getDark().intValue() - dakm));
		}

		if (cu.getGem().intValue() < gem) {
			throw new SignException("act_upgrade:not enough gem", 2);
		}
		cu.setGem(Integer.valueOf(cu.getGem().intValue() - gem));
		cm.setCcuser(cu);

		int exp = 0;
		if (strs[6].length() > 0) {
			exp = Integer.parseInt(strs[6]);
		}
		int time = Integer.parseInt(strs[3]);
		// 加工人队列,同时也加经验队列
		// 注意这边,工人队列可能的time可能是0或者负数,最好加完遍历一下
		// 到期的直接加经验(time=0或者-1)
		// 加一个工人完工时间
		cm.getWorkend().add(t + time * 1000L);
		// 同时也加一个经验和建筑物type
		cm.getExping().add(exp+"");
		cm.addWork(type);
		this.initBuildingStatus(cm, t,request);
		cm.setCcuser(cu);
		this.initBuildingStatus(cm, t, request);
	}

	// 检测升级前置条件
	private boolean CheckUpgrade(HttpServletRequest request, JSONObject ht,
			CcMap cm) {
		try {
			long t = Long.parseLong(getParamValue("t", ht, request));
			initBuildingStatus(cm, t, request);
			if (cm.getSlavs().size() <= cm.getWorkend().size())
				return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	

	// 检测建造前置条件
	// cm中workings,workend,exp3个list保持同步,既是同增同删
	// 每个位置一一对应 
	private boolean CheckBuild(HttpServletRequest request, JSONObject ht,
			CcMap cm) throws Exception {
			int lv = 1;
			String type = getParamValue("type", ht, request);
			// DECT的type直接是D1,2,3,4 xml中也如此保存
			long t = Long.parseLong(getParamValue("t", ht, request));
			// 检测工人完工情况
			initBuildingStatus(cm, t,request);
			Building bu = getBuildSettingXML(request).findBuByID(type);
			if (bu == null) {
				return false;
			}
			// 类型;需求等级(HT/User);花费;建造时间;矿脉容量/dect总数;生产速率;建造经验;加速费用;
			String str = (String) bu.getLv().get(lv - 1);
			System.out.println("str:"+str);
			String[] strs = str.split(";");
			int exp = 0;
			if (strs[6].length() > 0) {
				exp = Integer.parseInt(strs[6]);
			}
			int needtime = Integer.parseInt(strs[3]);
			// -1不需要工人,否则需要工人
			if ((needtime != -1)
					&& (cm.getSlavs().size() <= cm.getWorkend().size())) {
				return false;
			}
			CcUser cu = cm.getCcuser();
			// 该级可以拥有数量
			String[] count = bu.getCount().split(",");
			int curhtlv = cu.getHtlv().intValue();
			// 花费
			String[] cost = strs[2].split(",");
			int gold = 0;
			int elxm = 0;
			int dakm = 0;
			int gem = 0;
			// 奴隶造价特殊
			if (type.equals("SLAV")) {
				if (cm.getSlavs().size() == 0)
					gem = Integer.parseInt(cost[0].substring(1));
				else if (cm.getSlavs().size() == 1)
					gem = Integer.parseInt(cost[1]);
				else if (cm.getSlavs().size() == 2)
					gem = Integer.parseInt(cost[2]);
				else if (cm.getSlavs().size() == 3)
					gem = Integer.parseInt(cost[3]);
				else if (cm.getSlavs().size() == 4)
					gem = Integer.parseInt(cost[4]);
			} else {
				for (int i = 0; i < cost.length; i++) {
					if (cost[i].startsWith("g"))
						gold = Integer.parseInt(cost[i].substring(1));
					else if (cost[i].startsWith("e"))
						elxm = Integer.parseInt(cost[i].substring(1));
					else if (cost[i].startsWith("d"))
						dakm = Integer.parseInt(cost[i].substring(1));
					else if (cost[i].startsWith("m")) {
						gem = Integer.parseInt(cost[i].substring(1));
					}
				}
			}
			// 建造时间
			int time = Integer.parseInt(strs[3]);
			// 0级时候只能造城镇中心或者奴隶
			if ((curhtlv == 0) && (type.equals("HALL"))) {
				if (cm.getHall() != null) {
					return false;
				}
				if (cu.getElixir().intValue() < elxm) {
					return false;
				}
				cu.setGold(Integer.valueOf(cu.getGold().intValue() - gold));
				cm.setCcuser(cu);
				// 加一个工人完工时间
				cm.getWorkend().add(t + time * 1000L);
				// 同时也加一个经验和建筑物type
				cm.getExping().add(exp+"");
				cm.addWork(type);
				return true;
			}
			// 0级时候只能造城镇中心或者奴隶
			if ((curhtlv == 0) && (type.equals("SLAV"))) {
				if (cu.getGem().intValue() < gem) {
					return false;
				}
				cu.setGem(Integer.valueOf(cu.getGem().intValue() - gem));
				cm.setCcuser(cu);
				// 加一个工人完工时间
				cm.getWorkend().add(t + time * 1000L);
				// 同时也加一个经验和建筑物type
				cm.getExping().add(exp+"");
				cm.addWork(type);
				return true;
			}
			int maxcount = 0;
			// DECT数量特殊
			if (type.length() < 4 && type.startsWith("D")) {
				maxcount = Integer.parseInt(cost[4]);
			} else {
				maxcount = Integer.parseInt(count[(curhtlv - 1)]);
			}
			
			if (getCurCount(cm, type) >= maxcount) {
				return false;
			}
			int htlv = Integer.parseInt(strs[1]);
			if (htlv > cu.getHtlv().intValue()) {
				return false;
			}
			if (cu.getGold().intValue() < gold) {
				// 资源永远只有一种,非gem资源不够直接扣gem
				int addgem = this.getNeedGemFromGold(request, (gold - cu.getGold()
						.intValue()));
				if (cu.getGem().intValue() < addgem) {
					throw new SignException("act_build:not enough gold and gem",
							2);
				} else {
					cu.setGold(0);
					cu.setGem(Integer.valueOf(cu.getGem().intValue() - addgem));
				}
			} else {
				cu.setGold(Integer.valueOf(cu.getGold().intValue() - gold));
			}

			if (cu.getElixir().intValue() < elxm) {
				int addgem = this.getNeedGemFromElxm(request, (elxm - cu
						.getElixir().intValue()));
				if (cu.getGem().intValue() < addgem) {
					throw new SignException("act_build:not enough elxm and gem",
							2);
				} else {
					cu.setElixir(0);
					cu.setGem(Integer.valueOf(cu.getGem().intValue() - addgem));
				}
			} else {
				cu.setElixir(Integer.valueOf(cu.getElixir().intValue() - elxm));
			}

			if (cu.getDark().intValue() < dakm) {
				int addgem = this.getNeedGemFromDakm(request, (dakm - cu.getDark()
						.intValue()));
				if (cu.getGem().intValue() < addgem) {
					throw new SignException("act_build:not enough dakm and gem",
							2);
				} else {
					cu.setDark(0);
					cu.setGem(Integer.valueOf(cu.getGem().intValue() - addgem));
				}
			} else {
				cu.setDark(Integer.valueOf(cu.getDark().intValue() - dakm));
			}

			if (cu.getGem().intValue() < gem) {
				throw new SignException("act_build:not enough gem", 2);
			}
			cu.setGem(Integer.valueOf(cu.getGem().intValue() - gem));
			cm.setCcuser(cu);
			
			// 加工人队列,同时也加经验队列
			// 注意这边,工人队列可能的time可能是0或者负数,最好加完遍历一下
			// 到期的直接加经验(time=0或者-1)
			// 加一个工人完工时间
			cm.getWorkend().add(t + time * 1000L);
			// 同时也加一个经验和建筑物type
			cm.getExping().add(exp+"");
			cm.addWork(type);
			this.initBuildingStatus(cm, t,request);
			cm.setCcuser(cu);
		
		return true;
	}

	// 检测工人完工情况
	// 每个需要工人的建造/升级,都把完工时间压入List底部
	// 下次需要工人的事件时候遍历,把完工的时间移除
	private void initBuildingStatus(CcMap cm, long t, HttpServletRequest request) {
		List<Long> list = cm.getWorkend();
		List<Long> list2 = new ArrayList<Long>();
		//完工建筑物队列,增加经验队列
		List<String> list3 = new ArrayList<String>();
		List<String> list4 = new ArrayList<String>();
		for (int i = 0; i < list.size(); i++) {
			// 完工的加入list2
			if (list.get(i) <= t) {
				list2.add(list.get(i));
				list3.add(cm.getWorkings().get(i));
				list4.add(cm.getExping().get(i));
			}
		}
		for (int i = 0; i < list2.size(); i++) {
			cm.removeWorkend(list2.get(i));
			this.checkUpgradeBuilding(cm, t, list3.get(i),list4.get(i), request);
		}
	}

	private int getCurCount(CcMap cm, String type) {
		if (type.equals("WALL"))
			return cm.getWalls().size();
		if (type.equals("SLAV"))
			return cm.getSlavs().size();
		if (type.equals("CAST")) {
			if (cm.getCast() != null) {
				return 1;
			}
			return 0;
		}
		if (type.equals("HALL")) {
			if (cm.getHall() != null) {
				return 1;
			}
			return 0;
		}
		if (type.equals("LABT")) {
			if (cm.getLabt() != null) {
				return 1;
			}
			return 0;
		}
		if (type.equals("SPEL")) {
			if (cm.getSpel() != null) {
				return 1;
			}
			return 0;
		}
		if (type.equals("KING")) {
			if (cm.getKing() != null) {
				return 1;
			}
			return 0;
		}
		if (type.equals("QUEN")) {
			if (cm.getQuen() != null) {
				return 1;
			}
			return 0;
		}
		if (type.equals("GOLM"))
			return cm.getGolms().size();
		if (type.equals("ELXM"))
			return cm.getElxms().size();
		if (type.equals("DAKM"))
			return cm.getDakms().size();
		if (type.equals("GOLS"))
			return cm.getGolss().size();
		if (type.equals("ELXS"))
			return cm.getElxss().size();
		if (type.equals("DAKS"))
			return cm.getDakss().size();
		if (type.equals("CAMP"))
			return cm.getCamps().size();
		if (type.equals("BARK"))
			return cm.getBarks().size();
		if (type.equals("BKDK"))
			return cm.getBkdks().size();
		if (type.equals("ARCT"))
			return cm.getArcts().size();
		if (type.equals("CANN"))
			return cm.getCanns().size();
		if (type.equals("MORT"))
			return cm.getMorts().size();
		if (type.equals("TSLA"))
			return cm.getTslas().size();
		if (type.equals("INFT"))
			return cm.getInfts().size();
		if (type.equals("XBOW"))
			return cm.getXbows().size();
		if (type.equals("WIZT"))
			return cm.getWizts().size();
		if (type.equals("AIRD"))
			return cm.getAirds().size();
		// DECT特殊
		if (type.length() < 4 && type.startsWith("D")) {
			int count = 0;
			List<Dect> list = cm.getDects().get(type);
			if (list != null) {
				count = list.size();
			}

			return count;
		}
		// TREE也特殊
		if (type.length() < 4 && type.startsWith("T")) {
			int count = 0;
			List<Tree> list = cm.getTrees().get(type);
			if (list != null) {
				count = list.size();
			}
			return count;
		}
		if (type.startsWith("BOMB"))
			return cm.getBombs().size();
		if (type.startsWith("TRAP"))
			return cm.getTraps().size();
		if (type.startsWith("GITB"))
			return cm.getGitbs().size();
		if (type.startsWith("AIRB"))
			return cm.getAirbs().size();
		if (type.startsWith("AIRM")) {
			return cm.getAirms().size();
		}
		return 0;
	}

}
